package spe.ui.editor.figure;

import org.eclipse.draw2d.ChopboxAnchor;
import org.eclipse.draw2d.ConnectionAnchor;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.ImageFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Rectangle;

public abstract class SPEFigureImpl extends Figure implements SPEFigure 
{
	protected Label nameLabel;
	protected ImageFigure imgFigure = null;
	protected ConnectionAnchor anchor = null;
	
	@Override
	public Label getNameLabel()
	{
		return nameLabel;
	}

	@Override
	public ConnectionAnchor getConnectionAnchor()
	{
		if(anchor == null)
			anchor = new ChopboxAnchor(this);
		return anchor;
	}
	
	@Override
	public Dimension getImageSize()
	{
		if( (imgFigure == null) || (imgFigure.getImage()==null) )
			return null;
		
		Dimension d = new Dimension(imgFigure.getImage().getBounds().width, imgFigure.getImage().getBounds().width);
		return d;
	}
	
	@Override
	protected void paintFigure(Graphics graphics) 
	{
		Rectangle r = getBounds().getCopy();
		
		int x = r.width/2 - imgFigure.getImage().getBounds().width/2;
		
		setConstraint(imgFigure, new Rectangle(x, 0, -1, -1));
		
		x = r.width/2 - nameLabel.getTextBounds().width/2;
		setConstraint(nameLabel, new Rectangle(x, imgFigure.getImage().getBounds().height+2, -1, -1));
		
		imgFigure.invalidate();
		nameLabel.invalidate();
	}

}
